<?php
//企业展示页面后台控制器
namespace Admin\Controller;
use       Think\Controller;

class CompanyController extends Controller {
    /**
     * 后台首页
     */
    public function index() {
        $this->display();
    }

    /**
     * 栏目列表
     */
    public function catList() {
        $companycats = M('CompanyCats');
        //查询对应字段，在模板展示
        $list        = $companycats->field('cat_id, cat_name, news_number, cat_addtime, cat_updatetime')->select();
        $this->assign('list', $list);
        $this->display();
    }

    /**
     * 添加栏目
     */
    public function catAdd() {
        if (empty($_POST)) {
            //无表单提交，展示空白页面
            $this->display();
        } else {
            $companycats = D('CompanyCats');
            //检测提交栏目是否为空
            if (I('post.cat_name') == '') {
                header('refresh:2;url='.U('/admin/company/catAdd'));
                $this->feedback(2, '栏目名称不能为空！');
            }
            //自动验证内容并作相应跳转
            if ($companycats->create()) {
                $companycats->add();
                header('refresh:2;url='.U('/admin/company/catList'));
                $this->feedback(1, '添加栏目成功!');
            } else {
                header('refresh:2;url='.U('/admin/company/catAdd'));
                $this->feedback(2, '栏目名称重复，请重新输入！');
            }
        }
    }

    /**
     * 栏目编辑
     */
    public function catEdit() {
        //无地址栏参数跳转后台首页
        if (empty($_GET)) {
            return redirect(U('/admin/company/index'));
        }
        $companycats = M('CompanyCats');
        $cat_id      = I('get.cat_id');
        if (empty($_POST)) {
            $row = $companycats->find($cat_id);
            //查不到对应数据跳转后台首页
            if (!$row) {
                return redirect(U('/admin/company/index'));
            }
            $this->assign('cat', $row);
            $this->display();
        } else {
            $cat_name = I('post.cat_name');
            if (!$companycats->where('cat_name = \''.$cat_name.'\' and cat_id !='.$cat_id)->find()) {
                $companycats->where('cat_id='.$cat_id)->save(array('cat_name'=>$cat_name));
                header('refresh:2;url='.U('/admin/company/catList'));
                $this->feedback(1, '栏目修改成功！');
            } else {
                header('refresh:2;url='.U('/admin/company/catEdit', array('cat_id'=>$cat_id)));
                $this->feedback(2, '栏目名称重复了，换一个试试看！');
            }

        }
    }

    /**
     * 栏目删除
     */
    public function catDel() {
        //无地址栏参数跳转后台首页
        if (empty($_GET)) {
            return redirect(U('/admin/company/index'));
        }
        $cat_id = I('get.cat_id');
        if ($cat_id == 1) {
            header('refresh:2;url='.U('/admin/company/catList'));
            $this->feedback(2, '默认栏目不能删除！');
        }
        $companycats = M('CompanyCats');
        $row = $companycats->find($cat_id);
        //查不到对应数据跳转后台首页
        if (!$row) {
            return redirect(U('/admin/company/index'));
        }
        if ($row['news_number'] != 0) {
            header('refresh:2;url='.U('/admin/company/catList'));
            $this->feedback(2, '栏目下还有文章，不能删除！');
        }
        //删除栏目并作相应跳转
        $companycats->delete($cat_id);
        return redirect(U('/admin/company/catList'));
    }

    /**
     * 新闻列表
     */
    public function newsList() {
        $companynews = D('CompanyNews');
        $list        = $companynews->field('news_id, news_title, company_news.cat_id, cat_name, news_content, news_addtime, news_updatetime')->join('company_cats ON company_cats.cat_id = company_news.cat_id')->order('news_id desc')->select();
        $this->assign('list', $list);
        $this->display();
    }

    /**
     * 添加新闻
     */
    public function newsAdd() {
        $companycats = D('CompanyCats');
        if (empty($_POST)) {
            $catlist = $companycats->select();
            $this->assign('catlist', $catlist);
            $this->display();
        } else {
            $data                       = I('post.');
            $companynews                = D('CompanyNews');
            $companynews->news_title    = $data['news_title'];
            $companynews->cat_id        = $data['cat_id'];
            $companynews->news_content  = $data['editorValue'];
            $companynews->news_addtime  = $companynews->news_updatetime = time();
            //栏目表下对应最后更新时间以及新闻数量更新
            $news_number                = $companycats->find($data['cat_id'])['news_number'] + 1;
            $companycats->where('cat_id ='.$data['cat_id'])->save(array('cat_updatetime'=>$companynews->news_updatetime, 'news_number'=>$news_number));
            //判断是否添加成功并作相应跳转
            if ($companynews->add()) {
                header('refresh:2;url='.U('/admin/company/newsList'));
                $this->feedback(1, '添加新闻成功！');
            } else {
                header('refresh:2;url='.U('/admin/company/newsAdd'));
                $this->feedback(2, '添加新闻失败！');
            }
        }
    }

    /**
     * 编辑新闻
     */
    public function newsEdit() {
        //无地址栏参数跳转后台首页
        if (empty($_GET)) {
            return redirect(U('/admin/company/index'));
        }
        if (empty($_POST)) {
            $row = M('CompanyNews')->find(I('get.news_id'));
            //查不到对应数据跳转后台首页
            if (!$row) {
                return redirect(U('/admin/company/index'));
            }
            $catlist = M('CompanyCats')->field('cat_id, cat_name, news_number, cat_addtime, cat_updatetime')->select();
            $this->assign('l', $row);
            $this->assign('catlist', $catlist);
            $this->display();
        } else {
            $row                  = M('CompanyNews')->where('news_id='.I('get.news_id'));
            $oldcat_id            = $row->find()['cat_id'];
            $data                 = I('post.');
            $updatetime           = time();
            $row->news_title      = $data['news_title'];
            $row->news_content    = htmlspecialchars_decode($data['editorValue']);
            $row->news_updatetime = $updatetime;
            $newcat_id            = $row->cat_id = $data['cat_id'];
            $companycats          = M('CompanyCats');
            //更新对应栏目中新闻数量并更新时间
            $newnum               = $companycats->find($newcat_id)['news_number'] + 1;
            $oldnum               = $companycats->find($oldcat_id)['news_number'] - 1;
            $companycats->where('cat_id='.$newcat_id)->save(array('news_number'=>$newnum, 'cat_updatetime'=>$updatetime));
            $companycats->where('cat_id='.$oldcat_id)->save(array('news_number'=>$oldnum, 'cat_updatetime'=>$updatetime));
            $row->save();
            return redirect('/admin/company/newsList');
        }
    }

    /**
     * 删除新闻
     */
    public function newsDel() {
        //无地址栏参数跳转后台首页
        if (empty($_GET)) {
            return redirect(U('/admin/company/index'));
        }
        $news_id = I('get.news_id');
        if ($news_id == -1) {
            M('CompanyNews')->where('news_id>0')->delete();
            return redirect(U('/admin/company/newslist'));
        }
        $row     = M('CompanyNews')->find($news_id);
        //查不到对应数据跳转后台首页
        if (!$row) {
            return redirect(U('/admin/company/index'));
        }
        //更新对应栏目下新闻数量以及最后更新时间
        $cat_id                     = $row['cat_id'];
        $companycats                = D('CompanyCats');
        $news_number                = $companycats->find($cat_id)['news_number'] - 1;
        $cat_updatetime             = time();
        $companycats->where('cat_id ='.$cat_id)->save(array('news_number'=>$news_number, 'cat_updatetime'=>$cat_updatetime));
        //删除对应新闻并作相应跳转
        M('CompanyNews')->delete($news_id);
        return redirect(U('/admin/company/newsList'));
    }

    /**
     * 用户列表
     */
    public function userList() {
        $this->display();
    }

    /**
     * 留言列表
     */
    public function msgList() {
        $msglist = M('CompanyMsg')->order('msg_id desc')->select();
        $this->assign('msglist', $msglist);
        $this->display();
    }

    /**
     * 删除留言
     */
    public function msgDel() {
        //无地址栏参数跳转后台首页
        if (!($data = $this->getParam())) {
            return redirect(U('/admin/company/index'));
        }
        $companymsg = M('CompanyMsg');
        if ($data->msg_id == -1) {
            $companymsg->where('msg_id>0')->delete();
            return redirect(U('/admin/company/msglist'));
        }
        $row = $companymsg->find($data->msg_id);
        //查不到对应数据跳转后台首页
        if (!$row) {
            return redirect(U('/admin/company/index'));
        }
        $companymsg->delete($data->msg_id);
        return redirect(U('/admin/company/msglist'));
    }

    /**
     * 查看留言
     */
    public function msgView() {
        //无地址栏参数跳转后台首页
        if (!($data = $this->getParam())) {
            return redirect(U('/admin/company/index'));
        }
        $companymsg = M('CompanyMsg');
        $row = $companymsg->find($data->msg_id);
        //查不到对应数据跳转后台首页
        if (!$row) {
            return redirect(U('/admin/company/index'));
        }
        $this->assign('m', $companymsg->find($data->msg_id));
        $this->display();
    }

    /**
     * 消息反馈
     * @param    $status    int    默认为0，反应消息对错颜色，1为正确，2为错误
     * @param    $msg       string 消息内容
     */
    public function feedback($status = 0, $msg = '') {
        if ($status == 0 || $msg == '') {
            return redirect(U('/admin/company/index'));
        }
        $this->assign('status', $status);
        $this->assign('msg', $msg);
        $this->display('feedback');
        exit();
    }

    /**
     * 通过get方法获得的参数
     * @return mixed 有参数时返回标准对象，无参数时返回false
     */
    protected function getParam() {
        $data = I('get.');
        if (empty($data)) {
            return false;
        }
        foreach ($data as $k=>$v) {
            $data[$k] = trim($v);
        }
        return (object)$data;
    }

    /**
     * 通过post方法获得的参数
     * @return mixed 有参数时返回标准对象，无参数时返回false
     */
    protected function postParam() {
        $data = I('post.');
        if (empty($data)) {
            return false;
        }
        foreach ($data as $k=>$v) {
            $data[$k] = trim($v);
        }
        return (object)$data;
    }
}